package com.nuotu.datawall.clouddata.layoutData;

import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.nuotu.datawall.clouddata.been.RtInfo;
import com.nuotu.datawall.clouddata.control.Token;
import com.nuotu.datawall.clouddata.util.EPieOptionUtil;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;
import com.nuotu.datawall.clouddata.view.EchartView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReaderCountPieLayout {
    private static volatile ReaderCountPieLayout instance;
    private static Context mContext;
    private static EchartView echartView;
    private int tempSize=0;
    private static List<Map> listMap;
    public static ReaderCountPieLayout getInstance(Context context) {
        if (instance == null) {
            synchronized (ReaderCountPieLayout.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new ReaderCountPieLayout();
                }
            }
        }
        return  instance;
    }
    public ReaderCountPieLayout init(EchartView echartView){
        this.echartView=echartView;
        listMap=new ArrayList<>();
////        echartView.setWebViewClient(new WebViewClient(){
////            @Override
////            public void onPageFinished(WebView view, String url) {
////                super.onPageFinished(view, url);
////                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
////                refreshData();
////            }
////        });
        return instance;
    }
    public void show(){
        x.task().post(new Runnable() {
            @Override
            public void run() {
                echartView.refreshEchartsWithOption(EPieOptionUtil.getPieOptions(listMap,true));
            }
        });
    }
    public void refreshData(){

        RequestParams params = Tool.getRequestParams(mContext, ServerConfig.POST_READERCOUNT_URL_KEY);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
               final RtInfo rtInfo= JsonUtil.fromJson(result, RtInfo.class);
                listMap=(List<Map>)rtInfo.getData();
//                x.task().autoPost(new Runnable() {
//                    @Override
//                    public void run() {
////                        echartView.refreshEchartsWithOption(EPieOptionUtil.getPieOptions((List<Map>)rtInfo.getData()));
//                        echartView.refreshEchartsWithOption(EPieOptionUtil.getPieOptions(listMap));
//                    }
//                });
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Token.getInstance(mContext).refreshToken(ex.toString());
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }

}
